<?php
/**
 * Created by PhpStorm.
 * User: tengwei
 * Date: 2018/9/7
 * Time: 上午11:32
 */

class PayHaimi extends M_Controller{

    //海米
    public function haimipay(){
        $oid =$this -> input -> get_post('oid');//course_order :id
        $order = $this->db->where('id',$oid)->get('course_order')->row_array();
        $courseId = $this->db->select('id')->where('order_id',$oid)->get('order_course')->row_array();
        $this->template->assign(array(
            'orders'=>$order,
            'courseid'=>$courseId
        ));
        $this->template->display("../gw/pay_haimi.html");
    }

    public function hmdata(){
        $oid           = $this->input->get_post('oid');//course_order :id
        $studentName   = $this->input->get_post('studentName');
        $applicantName = $this->input->get_post('applicantName');
        $projectNo     = $this->input->get_post('projectNo');//商户订单号
        $mobile        = $this->input->get_post('mobile');
        $applyAmt      = ($this->input->get_post('applyAmt'))*100;//借款金额
        $applyTerm     = $this->input->get_post('applyTerm');//借款期数
        $orderDate     = date(YmdHis,$this->input->get_post('orderDate'));
        $merProductNo  = $this->input->get_post('merProductNo');//商户平台产品编号
        $commodityName = $this->input->get_post('commodityName');//商户平台产品名称
        $contractNo    = $this->input->get_post('contractNo');//合约编号
        $homeAddress   = $this->input->get_post('homeAddress');
        $email         = $this->input->get_post('email');
//        $relation      = '本人';
//        $returnMerchantURL = 'https://www.baidu.com';

        $url = 'https://sdk.lovehaimi.com/sdk-web-haimi/merchant/open/submitOrder/vipabc';
        $appId= 'pa6fx3wyyyxz630az6v9k30sp6yb7pr4';
        $secretKey = 'lollzx0xwfy4j8n6n091uhyp7aqjgeupzu8cllascoz4l6tldd7rhd43ak3s586i';

        $str =
            "appId".$appId.
            "applicantName".$applicantName.
            "applyAmt".$applyAmt.
            "applyTerm".$applyTerm.
            "commodityName".$commodityName.
            "contractNo".$contractNo.
            "email".$email.
            "homeAddress".$homeAddress.
            "merProductNo".$merProductNo.
            "mobile".$mobile.
            "orderDate".$orderDate.
            "projectNo".$projectNo.
//            "relation".$relation.
//            "returnMerchantURL".$returnMerchantURL.
            "studentName".$studentName.$secretKey;

        $signvalue=MD5($str);//生成签名
        $arr = array(
            "appId",
            "studentName",
            "applicantName",
            "projectNo",
            "mobile",
            "applyAmt",
            "applyTerm",
            "orderDate",
            "merProductNo",
            "commodityName",
            "contractNo",
            "homeAddress",
            "email",
//            "relation",
//            "returnMerchantURL",
            "signvalue"
        );

        $arrinfo = compact($arr);
        $arrjson = json_encode($arrinfo);
        $rescode = $this -> curl_request($arrjson,$url);

        if($rescode == 0){
            $arrdata = array(
                'oid' => $oid,
                'studentName' => $studentName,
                'applicantName' => $applicantName,
                'projectNo' => $projectNo,
                'mobile' => $mobile,
                'applyAmt' => $applyAmt,
                'applyTerm' => $applyTerm,
                'orderDate' => $orderDate,
                'merProductNo' => $merProductNo,
                'contractNo' => $contractNo,
                'homeAddress' => $homeAddress,
                'email' => $email,
            );
            $this->db->insert("haimi_paydata",$arrdata);
        }

    }


    function curl_request($data,$url){
        $curl = curl_init();
        $header = array('User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36','X-AjaxPro-Method:ShowList','Content-Type: application/json', 'Content-Length:' . strlen($data));
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl,CURLOPT_POST,true);
        curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($curl,CURLOPT_CUSTOMREQUEST,'POST');
        if (!is_null($header)){
            curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
        }
        curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,60);
        curl_setopt($curl,CURLOPT_FOLLOWLOCATION,true);
        curl_setopt($curl,CURLOPT_TIMEOUT,10);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);
        $curl_get = curl_exec($curl);
        curl_close($curl);
        $arrdata=json_decode($curl_get,true);
        //print_r($arrdata);
        $rescode = $arrdata['code'];
        $hmurl = $arrdata['content'];
        if ($rescode == 0){
            //$this->msg("信息填写成功",dr_user_url($hmurl));
            header("Refresh:1;url=$hmurl");
            return $rescode;
        }elseif ($rescode == 1001){
            $this->msg("系统内部错误，请尽快联系海米管家");
        }elseif ($rescode == 30002){
            $this->msg("商户号不存在或不可用");
        }elseif ($rescode == 30003){
            $this->msg("参数校验失败,请重新正确填写");
        }
        return false;
    }



}